package com.htfyun.ft;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.htfyun.serialport.ServoSingleton;
import com.htfyun.serialport.uartservo.OnServoListener;
import com.htfyun.serialport.uartservo.ServoControlAttr;
import com.htfyun.serialport.uartservo.ServoErrorAttr;
import com.htfyun.serialport.uartservo.ServoInterestedInfo;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

public class ServoTuneActivity extends BaseActivity implements OnServoListener {

    private final int TUNE_MIN_VERSION = 58;

    private TextView txtTuneTips;
    private TextView txtTuneInfo;
    private Button btnTune;
    private View txtReturn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_servo_tune);

        txtTuneTips = (TextView) findViewById(R.id.txtTuneTips);
        txtTuneInfo = (TextView) findViewById(R.id.txtTuneInfo);
        txtTuneInfo.setText("");

        btnTune = findViewById(R.id.btnTune);
        btnTune.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setEnabled(false);
                tune();
            }
        });

        btnTune.setEnabled(false);

        txtReturn = findViewById(R.id.txtReturn);
        txtReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityCompat.finishAfterTransition(ServoTuneActivity.this);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        ServoSingleton.INSTANCE.getServoControl().registerServoListener(this);
        getHandler().sendEmptyMessage(MSG_GET_FW_VERSION);


    }

    @Override
    protected void onPause() {
        super.onPause();
        ServoSingleton.INSTANCE.getServoControl().unregisterServoListener(this);
        ServoSingleton.INSTANCE.getServoControl().servoStop();
        getHandler().removeMessages(MSG_GET_FW_VERSION);
        getHandler().removeMessages(MSG_SET_ZERO_CHECK);

        finish();
    }

    private void tune() {
        ServoSingleton.INSTANCE.getServoControl().servoStop();
        ServoSingleton.INSTANCE.getServoControl().setZeroForServo();
    }

    private void updateFirmwareVersion() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                int curVersion = ServoSingleton.INSTANCE.getServoControl().getServoInterestedInfo().getFirmwareVersion();
                String txt = getString(R.string.servo_tune_tips, curVersion, TUNE_MIN_VERSION);

                txtTuneTips.setText(txt);


                if (curVersion >= TUNE_MIN_VERSION) {
                    btnTune.setEnabled(true);
                }

            }
        });
    }

    @Override
    public void onServoListener(List<ServoErrorAttr> errorAttrList, ServoControlAttr controlAttr, ServoControlAttr.Instruction instruction, ServoInterestedInfo info) {

        switch (controlAttr) {
            case firmware_version:
                updateFirmwareVersion();
                getHandler().removeMessages(MSG_GET_FW_VERSION);
                break;
            case set_zero:
                if (instruction == ServoControlAttr.Instruction.get) {
                    if (!info.isSetZeroDoing()) {
                        getHandler().sendEmptyMessage(MSG_SET_ZERO_STOP);
                    }

                } else if (instruction == ServoControlAttr.Instruction.set) {
                    getHandler().sendEmptyMessage(MSG_SET_ZERO_START);
                }
                break;
        }
    }

    private final int MSG_GET_FW_VERSION = 100;
    private final int PERIOD_GET_FW_VERSION = 300;
    private final int MSG_SET_ZERO_START = 101;
    private final int MSG_SET_ZERO_STOP = 102;
    private final int MSG_SET_ZERO_CHECK = 103;
    private final int PERIOD_SET_ZERO_CHECK = 2000;


    @Override
    protected void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case MSG_GET_FW_VERSION:
                ServoSingleton.INSTANCE.getServoControl().getFirmwareVersion();
                getHandler().sendEmptyMessageDelayed(MSG_GET_FW_VERSION, PERIOD_GET_FW_VERSION);
                break;
            case MSG_SET_ZERO_START:
                btnTune.setEnabled(false);
                txtReturn.setEnabled(false);
                FTFloatWindowService.setFloatLayoutShow(false);
                txtTuneInfo.setText(R.string.servo_tune_doing);
                getHandler().sendEmptyMessageDelayed(MSG_SET_ZERO_CHECK, PERIOD_SET_ZERO_CHECK);
                break;
            case MSG_SET_ZERO_STOP:
                btnTune.setEnabled(true);
                txtReturn.setEnabled(true);
                FTFloatWindowService.setFloatLayoutShow(true);
                txtTuneInfo.setText(R.string.servo_tune_done);
                getHandler().removeMessages(MSG_SET_ZERO_CHECK);
                break;
            case MSG_SET_ZERO_CHECK:
                ServoSingleton.INSTANCE.getServoControl().getZeroForServo();
                getHandler().sendEmptyMessageDelayed(MSG_SET_ZERO_CHECK, PERIOD_SET_ZERO_CHECK);
                break;
        }
    }
}
